package com.colin.commentservice.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.colin.bean.Comment;
import com.colin.commentservice.service.CommentService;
import com.colin.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/{videoId}")
    public R<List<Comment>> getCommentByVideoId(@PathVariable("videoId") Long videoId) {
        return commentService.getCommentById(videoId);
    }

    @DeleteMapping("/{commentId}")
    public R<Boolean> deleteComment(@PathVariable("commentId" ) Long commentId) {
        String loginId = (String) StpUtil.getLoginId();

        return commentService.deleteComment(commentId,loginId);
    }

    @PostMapping("/{videoId}")
    public R<Boolean> addComment(@RequestBody Comment comment,
                                 @PathVariable("videoId") Long videoId) {
        String loginId = (String) StpUtil.getLoginId();
        return commentService.addComment(videoId, comment,loginId);
    }

    @PostMapping("/{videoId}/{commentId}")
    public R<Boolean> addChildComment(@PathVariable("videoId") Long videoId,
                                      @PathVariable("commentId") Long commentId ,
                                      @RequestBody Comment comment) {
        String loginId = (String) StpUtil.getLoginId();
        return commentService.addChildComment(videoId, commentId, comment,loginId);
    }

    @PostMapping("/like/{videoId}/{commentId}")
    public R<Boolean> likeComment(@PathVariable("commentId") Long commentId,
                                  @PathVariable("videoId") Long videoId) {
        String loginId = (String) StpUtil.getLoginId();
        return commentService.likeComment(videoId, commentId,loginId);
    }
}
